package com.zte.purchase.control;

import com.zte.purchase.model.*;
import com.zte.purchase.server.IPurchaseServer;
import com.zte.util.PageModel;
import com.zte.util.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class PurchaseAction {

    @Autowired
    private IPurchaseServer purchaseServer;

    @RequestMapping("/purchase/addOnePurchase")
    @ResponseBody
    public ResultMessage addOnePurchase(@RequestBody CPurchaseAndDetails cpaConn){

        boolean isUpdate = purchaseServer.addOnePurchase(cpaConn);

        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        msg.setFlag(true);
        msg.setMsg("修改成功");
        msg.setData(isUpdate);

        return msg;
    }

    @RequestMapping("/purchase/updatePurchaseStatus")
    @ResponseBody
    public ResultMessage updatePurchaseStatus(CPurchaseAndDetails cPurchase){
        System.out.println("985:"+cPurchase);

        boolean isUpdate = purchaseServer.updatePurchaseStatus(cPurchase);

        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        msg.setFlag(true);
        msg.setMsg("修改成功");
        msg.setData(isUpdate);

        return msg;
    }

    @RequestMapping("/purchase/deleteOnePurchase")
    @ResponseBody
    public ResultMessage deleteOnePurchase(String purchaseId){
        boolean isDelete = purchaseServer.deletePurchase(purchaseId);

        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        msg.setFlag(true);
        msg.setMsg("修改成功");
        msg.setData(isDelete);

        return msg;
    }

    @RequestMapping("/purchase/updatePurchase")
    @ResponseBody
    public ResultMessage updatePurchase(CPurchase cPurchase){
        boolean isUpdate = purchaseServer.updateOnePurchase(cPurchase);

        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        msg.setFlag(true);
        msg.setMsg("修改成功");
        msg.setData(isUpdate);

        return msg;
    }

    @RequestMapping("/purchase/queryPurchasePage")
    @ResponseBody
    public ResultMessage queryPurchasePage(Integer pageNum,Integer pageSize, PurchaseCondition conn){
        System.out.println(conn);
        PageModel<CPurchase> pageModel = new PageModel<CPurchase>();
        pageModel.setPageNum(pageNum);
        pageModel.setPageSize(pageSize);

        PageModel<CPurchase> model = purchaseServer.queryPurchasePage(pageModel, conn);

        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        msg.setMsg("查询成功");
        msg.setFlag(true);
        msg.setData(model);

        return msg;
    }

    @RequestMapping("/purchase/queryPurchaseDetails")
    @ResponseBody
    public ResultMessage queryPurchaseDetails(PurchaseCondition conn){
        System.out.println(conn);
        CPurchaseAndDetails cPurchaseAndDatails=null;
        if(conn.getPurchaseId()!=null&& !conn.getPurchaseId().equals(""))
            cPurchaseAndDatails= purchaseServer.queryPurchaseDatails(conn.getPurchaseId());

        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        msg.setMsg("查询成功");
        msg.setFlag(true);
        msg.setData(cPurchaseAndDatails);

        System.out.println(cPurchaseAndDatails);
        return msg;
    }

    @RequestMapping("/purchase/queryDetail2")
    @ResponseBody
    public CDetails2 queryPurchaseDetails(String pId){
        CDetails2 cDetails = purchaseServer.queryDetail2(pId);
        return cDetails;
    }


    @RequestMapping("/purchase/queryAllBrands")
    @ResponseBody
    public List<UpdateBrand> queryAllBrands(){
        List<UpdateBrand> brands = purchaseServer.queryAllBrands();
        return brands;
    }

    @RequestMapping("/purchase/updateSaveee")
    @ResponseBody
    @Transactional
    public boolean updateSaveee(UpdataSave updataSave){
        System.out.println("我是="+updataSave);
        int i = purchaseServer.updatePurppp(updataSave);
        int i1 = purchaseServer.updateDettt(updataSave);
        if(i>0 && i1>0){
            return true;
        }
        return false;
    }

}
